package com.zhizuobiao.coura.stockhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhizuobiao.coura.adapter.WeatherDetailAdapter;
import com.zhizuobiao.coura.application.MyApplication;
import com.zhizuobiao.coura.weather.Weather;
import com.zhizuobiao.coura.weather.WeatherService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by coura on 2016/12/29.
 */

public class WeatherDetailActivity extends AppCompatActivity {

    @BindView(R.id.tv_weatherdetail_wendu)
    TextView tvWeatherdetailWendu;
    @BindView(R.id.tv_weatherdetail_pm)
    TextView tvWeatherdetailPm;
    @BindView(R.id.tv_weatherdetail_tianqi)
    TextView tvWeatherdetailTianqi;
    @BindView(R.id.lv_weather_detail)
    ListView lvWeatherDetail;
    @BindView(R.id.tv_weatherdetail_data)
    TextView tvWeatherdetailData;
    @BindView(R.id.tv_weatherdetail_city)
    TextView tvWeatherdetailCity;
    @BindView(R.id.tv_weatherdetail_wind)
    TextView tvWeatherdetailWind;

    private MyApplication myApplication;
    private WeatherService weatherService;
    private Weather weather;
    private WeatherDetailAdapter weatherDetailAdapter;
    private List<Weather.ResultsBean.IndexBean> indexBeanList;
    private String city;

    private static final String AK = "409eEic3j0WzAvoBNETnkiXwac6G87nu";
    private static final String OUTPUT = "json";
    private static final String MCODE = "9D:B5:4E:A6:A8:CF:E4:B6:B5:6C:48:41:96:22:89:C6:88:B3:BF:9A;com.zhizuobiao.coura.stockhelper";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todayweather_detail);
        ButterKnife.bind(this);
        loadDate();
    }

    private void loadDate() {
        Intent intent = getIntent();
        city = intent.getStringExtra("city");
        myApplication = (MyApplication) getApplication();
        weatherService = myApplication.getWeatherService();
        Call<Weather> weatherCall = weatherService.getWeather(AK, OUTPUT, MCODE, city);
        weatherCall.enqueue(new Callback<Weather>() {
            @Override
            public void onResponse(Call<Weather> call, Response<Weather> response) {
                weather = response.body();
                if (weather.getError() == 0) {
                    indexBeanList = weather.getResults().get(0).getIndex();
                    tvWeatherdetailCity.setText(city);
                    tvWeatherdetailData.setText(weather.getDate());
                    tvWeatherdetailPm.setText(weather.getResults().get(0).getPm25());
                    tvWeatherdetailWendu.setText(weather.getResults().get(0).getWeather_data().get(0).getTemperature());
                    tvWeatherdetailTianqi.setText(weather.getResults().get(0).getWeather_data().get(0).getWeather());
                    tvWeatherdetailWind.setText(weather.getResults().get(0).getWeather_data().get(0).getWind());
                    weatherDetailAdapter = new WeatherDetailAdapter(WeatherDetailActivity.this,indexBeanList);
                    lvWeatherDetail.setAdapter(weatherDetailAdapter);
                }
            }

            @Override
            public void onFailure(Call<Weather> call, Throwable t) {
                Toast.makeText(WeatherDetailActivity.this,"数据加载失败。。。",Toast.LENGTH_LONG).show();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onClick() {
        Intent intent = new Intent();
        intent.putExtra("city",city);
        setResult(RESULT_OK,intent);
        finish();
    }
}
